package com.canaan.business.service.basic.impl;

import java.util.Date;
import java.util.List;
import com.canaan.common.utils.DateUtils;
import com.canaan.common.utils.SecurityUtils;
import com.canaan.common.utils.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import javax.annotation.Resource;
import com.canaan.business.mapper.basic.OwnerMapper;
import com.canaan.business.domain.basic.Owner;
import com.canaan.business.service.basic.IOwnerService;
import com.canaan.common.annotation.TableName;
/**
 * 货主Service业务层处理
 *
 * @author canaan
 * @date 2025-03-24
 */
@Service
public class OwnerServiceImpl implements IOwnerService {
    @Resource
    private OwnerMapper ownerMapper;

    /**
     * 查询货主
     *
     * @param id 货主主键
     * @return 货主
     */
    @Override
    public Owner selectOwnerById(Long id) {
        return ownerMapper.selectOwnerById(id);
    }

    /**
     * 查询货主列表
     *
     * @param owner 货主
     * @return 货主
     */
    @Override
    public List<Owner> selectOwnerList(Owner owner) {
        return ownerMapper.selectOwnerList(owner);
    }

    /**
     * 新增货主
     *
     * @param owner 货主
     * @return 结果
     */
    @Override
    public int insertOwner(Owner owner) {
        if(StringUtils.isEmpty(owner.getCreateBy())) {
            owner.setCreateBy(SecurityUtils.getUsername());
        }
        owner.setCreateTime(DateUtils.getNowDate());
        return ownerMapper.insertOwner(owner);
    }

    /**
     * 批量新增货主
     *
     * @param ownerList 货主列表
     * @return 结果
     */
    @Override
    public int batchInsertOwner(List<Owner> ownerList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (Owner owner : ownerList){
            owner.setCreateBy(username);
            owner.setCreateTime(currerTime);
        }
        int insertNum = 0;
        for (int i=0; i<ownerList.size();) {
            int endIndex = i+500;
            if (endIndex > ownerList.size()) {
                endIndex = ownerList.size();
            }
            insertNum = insertNum + ownerMapper.batchInsertOwner(ownerList.subList(i, endIndex));
            i = endIndex;
        }
        return insertNum;
    }

    /**
     * 修改货主
     *
     * @param owner 货主
     * @return 结果
     */
    @Override
    public int updateOwner(Owner owner) {
        if(StringUtils.isEmpty(owner.getUpdateBy())) {
            owner.setUpdateBy(SecurityUtils.getUsername());
        }
        owner.setUpdateTime(DateUtils.getNowDate());
        return ownerMapper.updateOwner(owner);
    }

    /**
     * 批量修改货主
     *
     * @param ownerList 货主列表
     * @return 结果
     */
    @Override
    public int batchUpdateOwner(List<Owner> ownerList) {
        String username = SecurityUtils.getUsername();
        Date currerTime = DateUtils.getNowDate();
        for (Owner owner : ownerList){
            owner.setUpdateBy(username);
            owner.setUpdateTime(currerTime);
        }
        int updateNum = 0;
        for (int i=0; i<ownerList.size();) {
            int endIndex = i+500;
            if (endIndex > ownerList.size()) {
                endIndex = ownerList.size();
            }
            updateNum = updateNum + ownerMapper.batchUpdateOwner(ownerList.subList(i, endIndex));
            i = endIndex;
        }
        return updateNum;
    }

    /**
     * 批量删除货主
     *
     * @param ids 需要删除的货主主键集合
     * @return 结果
     */
    @Override
    public int deleteOwnerByIds(Long[] ids) {
        return ownerMapper.deleteOwnerByIds(ids);
    }

    /**
     * 删除货主信息
     *
     * @param id 货主主键
     * @return 结果
     */
    @Override
    public int deleteOwnerById(Long id) {
        return ownerMapper.deleteOwnerById(id);
    }
}
